package com.bawei.common.view

import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.ViewDataBinding
import com.bawei.base.view.BaseActivity
import com.bawei.common.event.CustomEvent
import com.bawei.common.vm.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Author: Jason Zhang
 * Date: 2025/4/22
 * Desc: 基础MVVM架构下的Activity
 */
abstract class BaseMvvmActivity<VDB: ViewDataBinding> : BaseActivity<VDB>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化View
        initViews()
        //加载数据
        loadData()
        //观察数据变化
        observeDataChange()
        //注册EventBus事件总线
        EventBus.getDefault().register(this)
    }

    override fun initViews() {

    }

    protected open fun loadData() {

    }

    protected open fun observeDataChange(){

    }

    /**
     * EventBus接收事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onReceiveEvent(event: CustomEvent?){
        if (event != null && event.target == this.javaClass.simpleName){
            handEvent(event)
        }
    }

    protected open fun handEvent(event: CustomEvent){
        //TODO
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }



}